/******************************************************************************
  Grog Engine sample app - v0.1 - (c)2007 Naicigam
  ------------------------------------------------
  Filename: Input.cpp
  Creation date: 03/09/2007
  Author: Juan Manuel Alvarez
 ******************************************************************************/

//------------------------------------------------------------------------------
#include "Input.h"
//------------------------------------------------------------------------------
using namespace leo;
//------------------------------------------------------------------------------
Input::Input()
{
	Reset();

	// Tomamos la posicion actual del mouse, esto se esta haciendo mediante winapi, lo logico seria mudarlo a DirectInput

	POINT CursorPos;
	GetCursorPos(&CursorPos);

	MouseScreenAbsPos[0] = CursorPos.x;
	MouseScreenAbsPos[1] = CursorPos.y;
	MouseScreenAbsPos[2] = 0;
}
//------------------------------------------------------------------------------
Input::~Input()
{
	
}
//------------------------------------------------------------------------------
void Input::UpdateMouse()
{
	// update the mouse buttons data  based on the previous acquire data
	for(int b=0;b<3;b++)
	{
		if(MouseBut[b])
		{
			if(!ButState[b])
			{
				MouseButClick[b]=true;
				ButState[b]=true;
			}
			else { MouseButClick[b]=false; }
		}
		else { if(ButState[b]) { ButState[b]=false; } }
	}
}
//------------------------------------------------------------------------------
void Input::UpdateKeyboard()
{
	//update the key events data based on the previous acquire data
	for(int k=0; k<256; k++)
	{
		if(KeyBuffer[k] != KeyPrevBuffer[k])
		{
			if(GetKeyDown(k)) 	{ KeyEvent[k] = KS_DOWN; }
			else 				{ KeyEvent[k] = KS_UP; }
		}
		else { KeyEvent[k] = KS_NONE; }
	}
}
//------------------------------------------------------------------------------
void Input::SetMousePos(long x, long y, long z)
{
	MouseRelPos[POS_X] = x - MouseScreenAbsPos[POS_X];
	MouseRelPos[POS_Y] = y - MouseScreenAbsPos[POS_Y];
	MouseRelPos[POS_Z] = z - MouseScreenAbsPos[POS_Z];

	MouseScreenAbsPos[POS_X] = x;
	MouseScreenAbsPos[POS_Y] = y;
	MouseScreenAbsPos[POS_Z] = z;
}
//------------------------------------------------------------------------------
void Input::EventClean() { ZeroMemory(&KeyEvent, sizeof(KeyEvent)); }
//------------------------------------------------------------------------------
void Input::Reset()
{
	ZeroMemory(&KeyBuffer, sizeof(KeyBuffer));
	ZeroMemory(&KeyPrevBuffer, sizeof(KeyPrevBuffer));
	ZeroMemory(&KeyEvent, sizeof(KeyEvent));

	for(int eje=0;eje<3;eje++)
	{
		MouseScreenAbsPos[eje] = 0;
		MouseRelPos[eje] = 0;
		MouseBut[eje] = 0;
		MouseButClick[eje] = false;
		ButState[eje] = false;
	}
}
//------------------------------------------------------------------------------
